package com.nageoffer.shortlink.admin.common.toolkit;

import java.util.Random;
/**
 * 分组id生成器
 */
public final class RandomGenerator {
    /** 可选字符集：0-9 + a-z */
    private static final String CHAR_POOL = "abcdefghijklmnopqrstuvwxyz0123456789";

    /** 默认随机码长度 */
    private static final int DEFAULT_LENGTH = 6;

    private static final Random RANDOM = new Random();

    /**
     * 生成默认长度（6位）的随机码
     *
     * @return 6位伪随机字符串
     */
    public static String generateRandomString() {
        return generateCode(DEFAULT_LENGTH);
    }

    /**
     * 生成指定长度的随机码
     *
     * @param length 长度
     * @return 指定长度的伪随机字符串
     */
    public static String generateCode(int length) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = RANDOM.nextInt(CHAR_POOL.length());
            sb.append(CHAR_POOL.charAt(index));
        }
        return sb.toString();
    }
}
